﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;

namespace AMSService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "AMSService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select AMSService.svc or AMSService.svc.cs at the Solution Explorer and start debugging.
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class AMSService : IAMSService
    {
        public AMSService()
        {

        }

        /* Yes, I know this validation method is really, really insecure because the
           validation credentials can be skimmed from the data stream and it makes no use
           of tokens or other enduring session validation techniques.  Happily that's not
           part of the assignment specification. */
        public model.User Login(string UserName, string Password)
        {
            model.User usr = new model.User(UserName, Password);

            if (usr.IsValid)
            {
                usr.LoadAssets();
                return usr;
            }
            else
                return null;
        }

        public bool Register(model.User CurrentUser)
        {
            if (CurrentUser != null && CurrentUser.Name.Length > 0)
            {
                if (CurrentUser.UserExists())
                    return false;
                else
                    return CurrentUser.Save();
            }
            else
                return false;
        }

        public model.AMSImage SaveImage(model.AMSImage CurrentImage)
        {
            if (CurrentImage.Save())
            {
                //
                return CurrentImage;
            }
            else
                return null;
        }

        public model.Asset SaveAsset(model.Asset CurrentAsset)
        {
            if (CurrentAsset.Save())
            {
                CurrentAsset.Owner.LoadAssets();
                return CurrentAsset;
            }
            else
                return null;
        }

        public model.PurchaseData SavePurchase(model.PurchaseData CurrentPurchase)
        {
            if (CurrentPurchase.Save())
            {
                CurrentPurchase.BaseItem.Owner.LoadAssets();
                return CurrentPurchase;
            }
            else
                return null;
        }

        public model.ScheduleData SaveSchedule(model.ScheduleData CurrentSchedule)
        {
            if (CurrentSchedule.Save())
            {
                CurrentSchedule.BaseItem.Owner.LoadAssets();
                return CurrentSchedule;
            }
            else
                return null;
        }

        public model.ServiceData SaveService(model.ServiceData CurrentService)
        {
            if (CurrentService.Save())
            {
                CurrentService.BaseItem.Owner.LoadAssets();
                return CurrentService;
            }
            else
                return null;
        }

        public model.User SaveUser(model.User CurrentUser)
        {
            if (CurrentUser.Save())
            {
                CurrentUser.LoadAssets();
                return CurrentUser;
            }
            else
                return null;
        }

        public model.WarrantyData SaveWarranty(model.WarrantyData CurrentWarranty)
        {
            if (CurrentWarranty.Save())
            {
                CurrentWarranty.BaseItem.Owner.LoadAssets();
                return CurrentWarranty;
            }
            else
                return null;
        }

        public bool DeleteImage(model.AMSImage CurrentImage)
        {
            return CurrentImage.Delete();
        }

        public bool DeleteAsset(model.Asset CurrentAsset)
        {
            return CurrentAsset.Delete();
        }

        public bool DeletePurchase(model.PurchaseData CurrentPurchase)
        {
            return CurrentPurchase.Delete();
        }

        public bool DeleteSchedule(model.ScheduleData CurrentSchedule)
        {
            return CurrentSchedule.Delete();
        }

        public bool DeleteService(model.ServiceData CurrentService)
        {
            return CurrentService.Delete();
        }

        public bool DeleteUser(model.User CurrentUser)
        {
            return CurrentUser.Delete();
        }

        public bool DeleteWarranty(model.WarrantyData CurrentWarranty)
        {
            return CurrentWarranty.Delete();
        }
    }
}
